package com.breakfast.fun.adapter.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.common.CommonCart;
import com.breakfast.fun.dao.ProductCart;
import com.breakfast.fun.event.PopEvent;
import com.breakfast.fun.util.DaoUtil;
import com.breakfast.fun.util.T;
import com.breakfast.fun.util.TimeUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.breakfast.fun.util.DaoUtil.getProductList;

/**
 * 底部弹出的购物车PopWindow中的list适配器
 * Created by yukai on 2016/10/28.
 */

public class PopCartAdapter extends BaseListAdapter<ProductCart> {

    private CommonCart mCommonCart = new CommonCart();
    private List<ProductCart> productCarts;

    public PopCartAdapter(Context context, List<ProductCart> list) {
        super(context, list);

        mCommonCart.init(context);
        productCarts = mCommonCart.productCartDao.loadAll();

        for (int i = 0; i < productCarts.size(); i++) {
            mCommonCart.totalPrice += productCarts.get(i).getTotalPrice();
            mCommonCart.totalNum += productCarts.get(i).getNum();
        }

        mCommonCart.event.setTotalPrice(mCommonCart.totalPrice);
        mCommonCart.event.setTotalNum(mCommonCart.totalNum);
        EventBus.getDefault().post(mCommonCart.event);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pop_cart, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ProductCart item = getItem(position);
        holder.mTvPopCartItemName.setText(item.getName());
        holder.mTvPopCartItemDate.setText(item.getDate() + "  " + TimeUtil.CalcuDate(item.getDate()));
        holder.mTvPopCartItemPrice.setText("￥" + String.format("%.2f", item.getTotalPrice()));
        holder.mTvPopCartItemNum.setText(item.getNum() + "");

        holder.mIvPopCartItemAdd.setOnClickListener(v -> {
            addProduct(item, holder);
        });

        holder.mIvPopCartItemSub.setOnClickListener(v -> {
            delProduct(item, holder);
        });

        return convertView;
    }

    /**
     * 删除的时候全局刷新购物车信息
     * 如果全删完了或清空了
     * 就让popWindow消失
     *
     * @param data
     * @param holder
     */
    private void delProduct(ProductCart data, ViewHolder holder) {
        List<ProductCart> productCartList = DaoUtil.getProductList(data.getCategoryId(), data.getProductId(), data.getDate());

        productCartList.get(0).setNum(productCartList.get(0).getNum() - 1);
        productCartList.get(0).setTotalPrice(productCartList.get(0).getPrice() * productCartList.get(0).getNum());
        holder.mTvPopCartItemPrice.setText("￥" + String.format("%.2f", productCartList.get(0).getTotalPrice()));
        DaoUtil.getProductCartDao().update(productCartList.get(0));
        holder.mTvPopCartItemNum.setText(productCartList.get(0).getNum() + "");
        mCommonCart.totalNum -= 1;
        mCommonCart.totalPrice -= data.getPrice();
        mCommonCart.event.setTotalNum(mCommonCart.totalNum);
        mCommonCart.event.setTotalPrice(mCommonCart.totalPrice);
        EventBus.getDefault().post(mCommonCart.event);

        if (productCartList.get(0).getNum() == 0) {
            DaoUtil.getProductCartDao().delete(productCartList.get(0));
            removeEntity(data);
            notifyDataSetChanged();
        }

        if (getCount() == 0) {
            EventBus.getDefault().post(new PopEvent("NormalProductActivity"));
        }
    }

    /**
     * 增加并刷新全局购物车信息
     *
     * @param data
     * @param holder
     */
    private void addProduct(ProductCart data, ViewHolder holder) {
        if (data.getProductClassId().equals("19")) {
            T.show("该商品仅限一份");
            return;
        }
        List<ProductCart> productCartList = getProductList(data.getCategoryId(), data.getProductId(), data.getDate());


        productCartList.get(0).setNum(productCartList.get(0).getNum() + 1);
        productCartList.get(0).setTotalPrice(productCartList.get(0).getPrice() * productCartList.get(0).getNum());
        holder.mTvPopCartItemPrice.setText("￥" + String.format("%.2f", productCartList.get(0).getTotalPrice()));
        DaoUtil.getProductCartDao().update(productCartList.get(0));
        holder.mTvPopCartItemNum.setText(productCartList.get(0).getNum() + "");
        mCommonCart.totalNum += 1;
        mCommonCart.totalPrice += data.getPrice();
        mCommonCart.event.setTotalNum(mCommonCart.totalNum);
        mCommonCart.event.setTotalPrice(mCommonCart.totalPrice);
        EventBus.getDefault().post(mCommonCart.event);
    }


    static class ViewHolder {
        @Bind(R.id.tv_pop_cart_item_name)
        TextView mTvPopCartItemName;
        @Bind(R.id.tv_pop_cart_item_date)
        TextView mTvPopCartItemDate;
        @Bind(R.id.tv_pop_cart_item_price)
        TextView mTvPopCartItemPrice;
        @Bind(R.id.iv_pop_cart_item_sub)
        ImageView mIvPopCartItemSub;
        @Bind(R.id.tv_pop_cart_item_num)
        TextView mTvPopCartItemNum;
        @Bind(R.id.iv_pop_cart_item_add)
        ImageView mIvPopCartItemAdd;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
